<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="周转卡编号">
          <el-input
            v-model="searchParam.turnoverCode"
            placeholder="请输入周转卡编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="图号">
          <el-input
            v-model="searchParam.figureNumber"
            placeholder="请输入图号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="批次">
          <el-input
            v-model="searchParam.materialBatch"
            placeholder="请输入批次"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="工序分类">
          <el-select
            v-model="searchParam.processesClassification"
            placeholder="请选择工序分类"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('ProcessType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="条码状态">
          <el-select
            v-model="searchParam.status"
            placeholder="请选择条码状态"
            clearable
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周转卡类型">
          <el-select
            v-model="searchParam.turnoverType"
            placeholder="请选择条码状态"
            clearable
          >
            <el-option
              v-for="item in dictionaryStore.filterDictList('TurnoverType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          label="序号"
          width="55"
          align="center"
          key="id"
        />

        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="工厂"
          prop="plantCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="周转卡编号"
          prop="turnoverCode"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品名称"
          prop="materialCode"
          min-width="180"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-if="row.materialCode"
              >{{ row.materialName }}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          label="图号"
          prop="figureNumber"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="批次号"
          prop="materialBatch"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="数量"
          prop="materialQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        >
          <template #default="{ row }">
            {{ row.materialQuantity || '' }}
          </template>
        </el-table-column>
        <el-table-column
          label="数量单位"
          prop="quantityUnit"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ useDictionary("MeasurementUnit", row.quantityUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <span v-if="row.workCenterCode">{{ row.workCenterCode }}</span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          label="线边库"
          prop="produceStorageCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工序"
          prop="processesCode"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ row.processesName }}
          </template>
        </el-table-column>
        <el-table-column
          label="工序分类"
          prop="processesClassification"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            {{ useDictionary("ProcessesClass", row.processesClassification) }}
          </template>
        </el-table-column>
        <el-table-column
          label="条码状态"
          prop="status"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.status === 1">初始打印</span>
            <span v-else-if="scope.row.status === 2">报工关联</span>
          </template>
        </el-table-column>
        <el-table-column
          label="周转卡类型"
          prop="turnoverType"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.turnoverType == 1">合格</span>
            <span v-else-if="scope.row.turnoverType == 2">不合格</span>
            <span v-else-if="scope.row.turnoverType == 3">返修</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <el-table-column fixed="right" label="操作" width="100" align="center">
          <template #default="scope">
            <!--<el-button
              type="primary"
              size="small"
              link
              @click="openDialogD(scope.row)"
              ><i-ep-search />
              详情
            </el-button>-->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesturnovercard-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <mesturnovercarddetail-edit
      :visible="addDialogDeltai.visible"
      :title="addDialogDeltai.title"
      :data="addDialogDeltai.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialogDeltai.readvisible"
      @change-visible="changeVisibleDetail"
    />

    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";

const dictionaryStore = useDictStoreHook();
defineOptions({
  name: "MESMESTURNOVERCARD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesturnovercard/queryByPage";
// const requestApi = "/mes/mesirondemanddetail/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  plantCode: "",
  status: "",
};

const deleteParam = {
  api: "/mes/mesturnovercard/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

const addDialogDeltai = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

async function getDetailD(id: String) {
  request({
    url: `/mes/mesturnovercard/get/${id}`,
    method: "get",
  })
    .then((data) => {
      console.log("data", data);
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

// 查询详情
async function getDetail(id: String) {
  request({
    url: `/mes/mesturnoverdetail/getByMainIdList?mainId=${id}`,
    method: "post",
    data: {},
  })
    .then((data) => {
      console.log(data.data);
      addDialogDeltai.data = { data: data.data };
      addDialogDeltai.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

async function getInfo(id: string) {
  request({
    url: `/mes/mesturnovercard/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      console.log("addDialog.data", addDialog.data);
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });

  console.log("addDialog.data", addDialog.data);
}

/** 打开周转卡表表单弹窗 */
function openDialog(roleObj?: Object) {
  console.log(roleObj);
  if (roleObj) {
    addDialog.title = "修改周转卡表";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增周转卡表";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开周转卡表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  console.log("abc");
  addDialogDeltai.title = "周转卡表详情";
  getDetail(roleObj.id);
  addDialogDeltai.readvisible = true;
}

function openDialogD(roleObj?: any) {
  addDialog.title = "周转卡表详情";
  console.log("roleObj", roleObj);
  getDetailD(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function changeVisibleDetail(value: boolean) {
  addDialogDeltai.visible = value;
  if (!value) {
    addDialogDeltai.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mesturnovercard/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
const statusList = [
  { name: "初始打印", id: "1" },
  { name: "报工关联", id: "2" },
];

const tableColumns = reactive([
  {
    prop: "plantCode",
    label: "工厂",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "turnoverCode",
    label: "周转卡编号",
    minWidth: 130,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialCode",
    label: "产品名称",
    minWidth: 180,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      scope.row.materialCode ? scope.row.materialName : "",
  },
  {
    prop: "figureNumber",
    label: "图号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialBatch",
    label: "批次号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialQuantity",
    label: "数量",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
    render: (scope: any) => scope.row.materialQuantity || "",
  },
  {
    prop: "quantityUnit",
    label: "数量单位",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      useDictionary("MeasurementUnit", scope.row.quantityUnit),
  },
  {
    prop: "workCenterCode",
    label: "工作中心",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      scope.row.workCenterCode ? scope.row.workCenterCode : "",
  },
  {
    prop: "produceStorageCode",
    label: "线边库",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "processesCode",
    label: "工序",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) => scope.row.processesName,
  },
  {
    prop: "processesClassification",
    label: "工序分类",
    minWidth: 100,
    isShow: true,
    align: "left",
    render: (scope: any) =>
      useDictionary("ProcessesClass", scope.row.processesClassification),
  },
  {
    prop: "status",
    label: "条码状态",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.status === 1
        ? "初始打印"
        : scope.row.status === 2
        ? "报工关联"
        : "",
  },
  {
    prop: "turnoverType",
    label: "周转卡类型",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.turnoverType == 1
        ? "合格"
        : scope.row.turnoverType == 2
        ? "不合格"
        : scope.row.turnoverType == 3
        ? "返修"
        : "",
  },
  {
    prop: "createTime",
    label: "创建时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "createdUserName",
    label: "创建人",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdateTime",
    label: "更新时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdatedUserName",
    label: "更新人",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
]);

const searchColumns = reactive([]);
</script>
